﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class TaskDialogRenameFile : Form
	{
		private Movie movie;

		public TaskDialogRenameFile()
		{
			InitializeComponent();
		}

		public TaskDialogRenameFile(Movie entity)
		{
			movie = entity;
			InitializeComponent();
		}

		private void TaskDialogRenameFile_Load(object sender, EventArgs e)
		{
			listBoxRenamingFormats.DataSource = Logic.Properties.Settings.Default.RenamingFileFormats;

			string[] filenames = movie.VideoInfo.FileName.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string filename in filenames)
			{
				System.IO.FileInfo info = new System.IO.FileInfo(filename);
				labelLocation.Text += info.Name + Environment.NewLine;
			}
		}

		private void listBoxRenamingFormats_SelectedIndexChanged(object sender, EventArgs e)
		{
			labelLocation.ResetText();
			string format = listBoxRenamingFormats.SelectedItem.ToString();

			string[] filenames = movie.VideoInfo.FileName.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

            List<string> newfilenames = new List<string>();

            int i = 1;
			foreach (string filename in filenames)
			{
                string part = "";
                if (filenames.Length > 1)
                    part = "-part" + i;

				string name = format;
				System.IO.FileInfo info = new System.IO.FileInfo(filename);
				name = name.Replace("{title}", movie.Title);
				name = name.Replace("{year}", movie.Year.ToString());
				name = name.Replace("{director}", movie.Director);

                name = name + part + info.Extension;
                i++;

				labelLocation.Text += name + Environment.NewLine;
			}
		}

		private void buttonAccept_Click(object sender, EventArgs e)
		{
			string format = listBoxRenamingFormats.SelectedItem.ToString();

			string[] filenames = movie.VideoInfo.FileName.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);

			List<string> newfilenames = new List<string>();

            int i = 1;
			foreach (string filename in filenames)
			{
                string part = "";
                if (filenames.Length > 1)
                    part = "-part" + i;

				string name = format;
				System.IO.FileInfo info = new System.IO.FileInfo(filename);
				name = name.Replace("{title}", movie.Title);
				name = name.Replace("{year}", movie.Year.ToString());
				name = name.Replace("{director}", movie.Director);
                name = name.Replace(": ", " - ").Replace("?", "").Replace("|", "").Replace("\\", "").Replace("/", " - ");

                string newfilename = System.IO.Path.Combine(info.DirectoryName, name + part + info.Extension);

				System.IO.File.Move(filename, newfilename);

				newfilenames.Add(newfilename);
                i++;
			}

			MovieBL mbl = new MovieBL();
			if (mbl.ChangeLocation(movie, newfilenames.ToArray()))
			{
				MessageBox.Show("Files renamed successfully", "Files renamed", MessageBoxButtons.OK, MessageBoxIcon.Information);
				DialogResult = DialogResult.OK;
			}
		}
	}
}
